﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CheckPageChanged.Properties;

namespace CheckPageChanged
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine();
			Console.WriteLine("CheckPageChanged ver {0}, Jan Vilimek (c) 2013", Assembly.GetExecutingAssembly().GetName().Version.ToString(2));
			Console.WriteLine("Checking: {0}", Settings.Default.url2test);

			var wc = new WebClient();
			var uri = new Uri(Settings.Default.url2test);
			wc.DownloadStringCompleted += wc_DownloadStringCompleted;
			while (true)
			{
				Console.WriteLine();
				Console.Write("Download started ({0})..", DateTime.Now);
				downloadFinished = false;
				lastException = null;
				wc.DownloadStringAsync(uri);
				while (!downloadFinished)
				{
					Console.Write(".");
					Thread.Sleep(1000);
				}
				if (lastException != null)
				{
					Console.ForegroundColor = ConsoleColor.Red;
					Console.WriteLine("Error ocured: {0}", lastException);
					Console.WriteLine();
					Console.ForegroundColor = ConsoleColor.Gray;
					Console.WriteLine("Please press enter to continue...");
					Console.ReadLine();
				}
				else
				{
					if (lastHash == null && currentHash != null)
						lastHash = currentHash;
					if (lastHash != null && currentHash != null && !currentHash.SequenceEqual(lastHash))
					{
						Console.ForegroundColor = ConsoleColor.Yellow;
						Console.WriteLine("Page changed!!!!");
						Process.Start(Settings.Default.url2test);
						Console.WriteLine();
						Console.ForegroundColor = ConsoleColor.Gray;
						Console.WriteLine("Please press enter to continue...");
						Console.ReadLine();
						lastHash = currentHash;
					}
				}
				Console.WriteLine();
				Console.ForegroundColor = ConsoleColor.Gray;
				Console.WriteLine("Now waiting for {0} seconds... Press CTRL+C to exit.", Settings.Default.waitTimeInSeconds);
				Thread.Sleep(Settings.Default.waitTimeInSeconds * 1000);
			}
		}
		static bool downloadFinished;
		static string lastString;
		static Exception lastException;
		static byte[] currentHash;
		static byte[] lastHash
		{
			get
			{
				if (!lastHash_readFromConfig)
				{
					lastHash_internal_do_not_change = String.IsNullOrWhiteSpace(Settings.Default.lastHash_internal_do_not_change)
						? null : Convert.FromBase64String(Settings.Default.lastHash_internal_do_not_change);
					lastHash_readFromConfig = true;
				}
				return lastHash_internal_do_not_change;
			}
			set
			{
				lastHash_readFromConfig = true;
				lastHash_internal_do_not_change = value;
				Settings.Default.lastHash_internal_do_not_change = Convert.ToBase64String(lastHash_internal_do_not_change);
				Settings.Default.Save();
			}
		}
		static bool lastHash_readFromConfig;
		static byte[] lastHash_internal_do_not_change;

		static SHA512 sha512o = SHA512Managed.Create();
		static Encoding strEncoder = Encoding.GetEncoding("windows-1250");

		static void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
		{
			lastString = e.Result;
			currentHash = sha512o.ComputeHash(strEncoder.GetBytes(lastString));
			lastException = e.Error;
			downloadFinished = true;
		}
	}
}
